//  Copyright (c) 2008  Yoong-ki Ahn & Youngjin Kim < mexif@googlegroups.com>
//  See the enclosed file COPYING for license information (MIT Licence).
//  If you did not receive this file,
//  see http://www.opensource.org/licenses/mit-license.php.




#include "stdafx.h"
#include "resource.h"
#include "exiftab.h"


class CexiftabModule : public CAtlDllModuleT< CexiftabModule >
{
public :
	DECLARE_LIBID(LIBID_exiftabLib)
	DECLARE_REGISTRY_APPID_RESOURCEID(IDR_EXIFTAB, "{942A139B-9BE7-482E-966D-9D4FD0994EC9}")
};

CexiftabModule _AtlModule;

class CexiftabApp : public CWinApp
{
public:

// Overrides
    virtual BOOL InitInstance();
    virtual int ExitInstance();

    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CexiftabApp, CWinApp)
END_MESSAGE_MAP()

CexiftabApp theApp;
extern HINSTANCE ghInst;

BOOL CexiftabApp::InitInstance()
{
	ghInst = _AtlBaseModule.GetResourceInstance();
    return CWinApp::InitInstance();
}

int CexiftabApp::ExitInstance()
{
    return CWinApp::ExitInstance();
}


// Used to determine whether the DLL can be unloaded by OLE
STDAPI DllCanUnloadNow(void)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    return (AfxDllCanUnloadNow()==S_OK && _AtlModule.GetLockCount()==0) ? S_OK : S_FALSE;
}


// Returns a class factory to create an object of the requested type
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
    return _AtlModule.DllGetClassObject(rclsid, riid, ppv);
}


// DllRegisterServer - Adds entries to the system registry
STDAPI DllRegisterServer(void)
{
	// If we're on NT, add ourselves to the list of approved shell extensions.

	// Note that you should *NEVER* use the overload of CRegKey::SetValue with
	// 4 parameters.  It lets you set a value in one call, without having to 
	// call CRegKey::Open() first.  However, that version of SetValue() has a
	// bug in that it requests KEY_ALL_ACCESS to the key.  That will fail if the
	// user is not an administrator.  (The code should request KEY_WRITE, which
	// is all that's necessary.)

	if ( 0 == (GetVersion() & 0x80000000UL) )
	{
		CRegKey reg;
		LONG    lRet;

		lRet = reg.Open ( HKEY_LOCAL_MACHINE,
			_T("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"),
			KEY_SET_VALUE );

		if ( ERROR_SUCCESS != lRet )
			return E_ACCESSDENIED;

		lRet = reg.SetValue ( _T("ExifTabExt Class"), 
			_T("{0A867738-3B88-450A-B9EE-671ED70E8BE1}") );

		if ( ERROR_SUCCESS != lRet )
			return E_ACCESSDENIED;
	}

	// registers object, typelib and all interfaces in typelib
	return _AtlModule.RegisterServer(FALSE);
}


// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer(void)
{
	// If we're on NT, remove ourselves from the list of approved shell extensions.
	// Note that if we get an error along the way, I don't bail out since I want
	// to do the normal ATL unregistration stuff too.

	if ( 0 == (GetVersion() & 0x80000000UL) )
	{
		CRegKey reg;
		LONG    lRet;

		lRet = reg.Open ( HKEY_LOCAL_MACHINE,
			_T("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"),
			KEY_SET_VALUE );

		if ( ERROR_SUCCESS == lRet )
		{
			lRet = reg.DeleteValue ( _T("{0A867738-3B88-450A-B9EE-671ED70E8BE1}") );
		}
	}

	return _AtlModule.UnregisterServer(FALSE);
}

